// pages/passwd/passwd.js
Page({

  data: {
    old: "",
    password: "",
    consistence: true
  },

  onLoad: function (options) {

  },

  handleOld(e) {
    this.setData({
      old: e.detail.value
    })
  },

  handlePassword(e) {
    this.setData({
      password: e.detail.value
    })
  },

  handleAgainBlur(e) {
    const { value } = e.detail;
    if (value == this.data.password) {
      this.setData({
        consistence: true
      })
    } else {
      this.setData({
        consistence: false
      })
    }
  },

  handleConfirm() {
    const user_info = wx.getStorageSync('user_info');
    if (!user_info) {
      wx.showModal({
        title: '您未登录',
        content: '是否跳转到登录界面？',
        success(res){
          if(res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login'
            })
          }
        }
      })
    }
    const data = {
      uid: user_info.uid,
      old_code: this.data.old,
      new_code: this.data.password
    }
    const res = wx.request({
      url: "http://47.94.130.28:8000/user/passwd/",
      data: data,
      method: "POST",
      success (res) {
        console.log(res)
        if (res.data.valid) {
          wx.removeStorageSync('user_info');
          wx.showToast({
            title: '密码修改成功！',
            icon: "success",
            mask: true
          }, 1500)
          setTimeout(() => {
            wx.reLaunch({
              url: '/pages/login/login',
            })
          }, 1500)
        } else {
          wx.showModal({
            title: "修改失败！",
            content: res.data.message,
            showCancel: false
          })
        }
      }
    })
    // const valid = false;
    // const message = "新密码不符合要求"
    // if (valid) {
    //   wx.showToast({
    //     title: '密码修改成功！',
    //     icon: "success",
    //     mask: true
    //   }, 1500)
    //   setTimeout(() => {
    //     wx.reLaunch({
    //       url: '/pages/login/login',
    //     })
    //   }, 1500)
    // } else {
    //   wx.showModal({
    //     title: "修改失败！",
    //     content: message,
    //     showCancel: false
    //   })
    // }
  }
})